package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.LigneDeCommande;
import model.Plat;
import model.Restaurant;

/* Cette classe s'occupe de l'affichage de la fen�tre qui affiche
 * les plats disponibles pour chaque restaurant  
 */
public class VuePlats extends JFrame{
	
	private boolean SelectionPlat=false;
	private JCheckBox []Menu =null;
	private JTextField [] Quantités;
	private JLabel [] prices;
	private int nbrPlat=0;
	private List<Plat> plats;
	private Restaurant Resto;
	private List<LigneDeCommande> choixCommande;
	private JPanel general=new JPanel(new GridLayout(1,4));
	private JPanel checkPanel = new JPanel(new GridLayout(0, 1));
	private JPanel labelPanel = new JPanel(new GridLayout(0, 1));
	private JPanel textPanel = new JPanel(new GridLayout(0,1));
	private static final long serialVersionUID = -3535118786934945940L;
	private static final int CANEVAS_HAUTEUR = 200;
	private static final int CANEVAS_LARGEUR = 400;
	private static final int MARGE_H = 50;
	private static final int MARGE_V = 60;
	
	int Somme=0;

	JButton btConfirmer= new JButton("Confirmer");
	 
	public VuePlats(Restaurant Resto){
		this.Resto=Resto;
		plats=Resto.getMenu();
		nbrPlat=plats.size();
		Menu = new JCheckBox [plats.size()];
		Quantités = new JTextField [plats.size()];
		prices = new JLabel [plats.size()];
		for(int i=0; i< Menu.length;i++){
			Menu[i]= new JCheckBox(plats.get(i).getDescription());
			Quantités[i] = new JTextField("entrer quantite");
			prices [i]= new JLabel(Integer.toString(plats.get(i).getPrice())+"$");
			Menu[i].setSelected(false);
			checkPanel.add(Menu[i]);
			textPanel.add(Quantités[i]);
			labelPanel.add(prices[i]);
			
		}
		general.add(checkPanel);
		general.add(textPanel);
		general.add(labelPanel);
		general.add(btConfirmer);
		
		//add(checkPanel, BorderLayout.LINE_START);
        add(general);
		
		UiSupport.createApplicationFrame(this, "Menu", 0, 0, CANEVAS_LARGEUR
				+ MARGE_H, CANEVAS_HAUTEUR + MARGE_V);
		setVisible(true);
	}

	public Map<Plat, Integer> getSelection (){
		Map<Plat, Integer> selection = new HashMap<Plat, Integer>();
		for(int i = 0; i < nbrPlat; i++){
			if(Menu[i].isSelected()){
				selection.put(plats.get(i), Integer.parseInt(Quantités[i].getText()));
			}
		}
		return selection;
	}
	
	public void addPlatSelectionListener(ActionListener listener){
		btConfirmer.addActionListener(listener);
	}

	
}
